﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Grizzy_X
{
    public partial class NewLayerForm : Form
    {
        public NewLayerForm()
        {
            InitializeComponent();
            //显示已经添加过的图层属性数据
            if (Controller.DBExists == true)
            {
                listView1.Items.Clear();
                Persistance.cmd.CommandText = "SELECT * FROM 已有图层属性数据";
                Persistance.myDataAdapter.SelectCommand = Persistance.cmd;
                DataSet myDataSet = new DataSet();
                Persistance.myConnection.Open();//打开链接，将已有图层属性数据读入
                int numberOfRows = Persistance.myDataAdapter.Fill(myDataSet, "已有图层属性数据");
                Persistance.myConnection.Close();
                DataTable myDataTable = myDataSet.Tables["已有图层属性数据"];
                if (numberOfRows == 0)
                {
                    listView1.Items.Add("暂无记录");
                }
                else listView1.Items.Clear();
                for(int i = 0;i<myDataTable.Rows.Count;i++)
                {
                    object[] temp = myDataTable.Rows[i].ItemArray.ToArray();
                    string[] attriValues = new string[4];
                    attriValues[0] = temp[0].ToString();
                    attriValues[1] = (string)temp[1];
                    attriValues[2] = (string)temp[2];
                    attriValues[3] = (string)temp[3];
                    ListViewItem value = new ListViewItem(attriValues);
                    listView1.Items.Add(value);
                }
            }
            else
            {
                listView1.Items.Add("暂未建立数据库。");
            }
        }
        public string layername;

        //确认创建新图层
        private void OKbutton_Click(object sender, EventArgs e)
        {
            layername = LnametextBox.Text;
            Controller.check = true;
            if (Controller.DBExists == true)
            {
                List<string[]> fields = new List<string[]>();
                for (int i = 0; i < EditAttri_DataGrid.Rows.Count; i++)
                {
                    string[] temp = new string[4];
                    temp[0] = EditAttri_DataGrid.Rows[i].Cells[0].Value.ToString();//字段名
                    temp[1] = EditAttri_DataGrid.Rows[i].Cells[1].Value.ToString();//字段类型
                    temp[2] = EditAttri_DataGrid.Rows[i].Cells[2].Value.ToString();//字段长度
                    temp[3] = EditAttri_DataGrid.Rows[i].Cells[3].Value.ToString();//字段描述
                    fields.Add(temp);
                }
                switch (LTypecomboBox.Text)
                {
                    case "点":
                        {
                            CPointLayer templayer1 = new CPointLayer();
                            templayer1.NewPointLayer(IDGenerator.LayerID(), LnametextBox.Text, Controller.MapPath, DescritextBox.Text);
                            templayer1.AttriFields = fields;
                            Controller.CurrentLayer = templayer1;
                            Controller.MyMap.Add(templayer1);
                            Persistance.cmd.CommandText = "CREATE TABLE " +
                                templayer1.Name +
                                " (" +
                                " 点对象ID int PRIMARY KEY ";
                            break;
                        }
                    case "线":
                        {
                            CLineLayer templayer2 = new CLineLayer();
                            templayer2.NewLineLayer(IDGenerator.LayerID(), LnametextBox.Text, Controller.MapPath, DescritextBox.Text);
                            templayer2.AttriFields = fields;
                            Controller.CurrentLayer = templayer2;
                            Controller.MyMap.Add(templayer2);
                            Persistance.cmd.CommandText = "CREATE TABLE "
                                + templayer2.Name +
                                 " (" +
                                 " 线对象ID int PRIMARY KEY ";
                            break;
                        }
                    case "多边形":
                        {
                            CPolygonLayer templayer3 = new CPolygonLayer();
                            templayer3.NewPolygonLayer(IDGenerator.LayerID(), LnametextBox.Text, Controller.MapPath, DescritextBox.Text);
                            templayer3.AttriFields = fields;
                            Controller.CurrentLayer = templayer3;
                            Controller.MyMap.Add(templayer3);
                            Persistance.cmd.CommandText = "CREATE TABLE " +
                                templayer3.Name +
                                " (" +
                                " 面对象ID int PRIMARY KEY";
                            break;
                        }
                    case "文本":
                        {
                            CTextLayer templayer4 = new CTextLayer();
                            templayer4.NewTextLayer(IDGenerator.LayerID(), LnametextBox.Text, Controller.MapPath, DescritextBox.Text);
                            templayer4.AttriFields = fields;
                            Controller.CurrentLayer = templayer4;
                            Controller.MyMap.Add(templayer4);
                            Persistance.cmd.CommandText = "CREATE TABLE " +
                                templayer4.Name +
                                " (" +
                                " 文本对象ID int PRIMARY KEY";
                            break;
                        }
                }

                //为新增图层创建一张属性表

                if (EditAttri_DataGrid.Rows.Count == 0)
                {
                    Persistance.cmd.CommandText += ");";
                }
                else
                {
                    Persistance.cmd.CommandText += ",";
                    for (int i = 0; i < EditAttri_DataGrid.Rows.Count; i++)
                    {
                        Persistance.cmd.CommandText += EditAttri_DataGrid.Rows[i].Cells[0].Value.ToString() + " ";
                        switch (EditAttri_DataGrid.Rows[i].Cells[1].Value.ToString())
                        {
                            case "整型(Int)":
                                Persistance.cmd.CommandText += "int";
                                if (EditAttri_DataGrid.Rows[i].Cells[3].Value.ToString().Contains("不允许为空"))
                                    Persistance.cmd.CommandText += " NOT NULL";
                                break;
                            case "字符串(String)":
                                Persistance.cmd.CommandText += "string";
                                string length = EditAttri_DataGrid.Rows[i].Cells[2].Value.ToString();
                                Persistance.cmd.CommandText += "(" + length + ")";
                                if (EditAttri_DataGrid.Rows[i].Cells[3].Value.ToString().Contains("不允许为空"))
                                    Persistance.cmd.CommandText += " NOT NULL";
                                break;
                            case "布尔型(Bool)":
                                Persistance.cmd.CommandText += "BIT,";
                                break;
                            case "浮点型(Float)":
                                Persistance.cmd.CommandText += "float";
                                if (EditAttri_DataGrid.Rows[i].Cells[3].Value.ToString().Contains("不允许为空"))
                                    Persistance.cmd.CommandText += " NOT NULL";
                                break;
                            case "比特型(Byte)":
                                Persistance.cmd.CommandText += "Byte";
                                if (EditAttri_DataGrid.Rows[i].Cells[3].Value.ToString().Contains("不允许为空"))
                                    Persistance.cmd.CommandText += " NOT NULL";
                                break;
                        }
                        if (i == EditAttri_DataGrid.Rows.Count - 1)
                            Persistance.cmd.CommandText += ");";
                        else
                            Persistance.cmd.CommandText += ",";
                    }
                }
                try
                {
                    Persistance.myConnection.Open();
                    int result = Persistance.cmd.ExecuteNonQuery();
                    Persistance.myConnection.Close();
                }
                catch
                {
                    MessageBox.Show("数据库发生错误！请检查输入。");
                    //MainForm.pMainWin.NewLayer.PerformClick();
                    return;
                }
                //至此,一张空的图层属性表已经建立
            }

            //将新建图层属性数据添加到数据库相应表中
            int currentLayerID = Controller.MyMap.Layer_List.Count - 1;
            if (Controller.DBExists == true)
            {
             
                Persistance.cmd.CommandText = "INSERT INTO 已有图层属性数据" +
                    "(图层ID, 图层名称,图层类型,描述)VALUES(" +
                    Controller.MyMap.Layer_List[currentLayerID].GetID() + ",'" + Controller.MyMap.Layer_List[currentLayerID].GetName + "','" + LTypecomboBox.Text + "','" + Controller.MyMap.Layer_List[currentLayerID].Description + "');";
                try
                {
                    Persistance.myConnection.Open();
                    int res = Persistance.cmd.ExecuteNonQuery();
                    Persistance.myConnection.Close();
                }
                catch
                {
                    MessageBox.Show("数据库发生错误！请检查输入。");
                }
            }
            this.Close();
        }

        private void NewLayerForm_Activated(object sender, EventArgs e)
        {
            LnametextBox.Text = "新建图层" + (Controller.CountLayer + 1).ToString();
            LnametextBox.Focus();
            if (Controller.DBExists == false)
            {
                AttritypedataGrid.Enabled = false;
                addfieldbutton.Enabled = false;

                NewLayertab.SelectedTab = LayerProperty;
               // DisplayAll.Tag = false;
            }
            else
            {
            }
            
            EditcheckBox.Checked = true;
        }

        private void NewLayertab_Selecting(object sender, TabControlCancelEventArgs e)
        {
            if (!Convert.ToBoolean(e.TabPage.Tag)&&Controller.DBExists==false)
                e.Cancel = true;
        }

        //增加字段
        private void addfieldbutton_Click(object sender, EventArgs e)
        {
            NewField newField = new NewField();
            newField.ShowDialog();
            if (newField.needAdd == true)
            {
                EditAttri_DataGrid.Rows.Add(newField .field_Name, newField.field_Type,  newField.field_Length, newField.field_Des);
            }
            else
            {
            }

        }

   
      
        
    }
}
